//
//  ZHONGJIANViewController.m
//  GOGOTravel
//
//  Created by dllo on 16/2/29.
//  Copyright © 2016年 果果. All rights reserved.
//

#import "ZHONGJIANViewController.h"
#import "CityModel.h"
#import "MoreCollectionViewCell.h"
#import "FuJinViewController.h"
@interface ZHONGJIANViewController ()<UICollectionViewDataSource, UICollectionViewDelegate>

@property (nonatomic, retain) NSMutableArray *arr;
@property (nonatomic, retain) BaseCollectionView *collection;
@end

@implementation ZHONGJIANViewController

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
    }
    return self;
}

- (void)setUrl:(NSString *)url{
    
    if (_url != url) {
        [_url release];
        _url = [url retain];
    }
    
}

- (void)dealloc{
    [_arr release];
    [_collection release];
    [_url release];
    [super dealloc];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor blackColor];
    
    
    
    
    [self handleData];
    
    [self createCollection];
}


- (void)createCollection{
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    layout.minimumInteritemSpacing = 0;
    layout.minimumLineSpacing = 0;
    layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    layout.itemSize = CGSizeMake(self.view.frame.size.width, self.view.frame.size.height /2);
    
    
    
    self.collection = [[BaseCollectionView alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height/4, self.view.frame.size.width, self.view.frame.size.height/2) collectionViewLayout:layout];
    _collection.delegate = self;
    _collection.dataSource = self;
    _collection.pagingEnabled = YES;
    _collection.backgroundColor = [UIColor blackColor];
    
    [self.view addSubview:_collection];
    [_collection release];
    
    [_collection registerClass:[MoreCollectionViewCell class] forCellWithReuseIdentifier:@"MoreCollectionViewCell"];

}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    CityModel *model = _arr[indexPath.row];
    
    FuJinViewController *fujin = [[FuJinViewController alloc] init];
    
    fujin.location = model.location;
    fujin.name = model.name;
    [self.navigationController pushViewController:fujin animated:YES];
    
    [fujin release];
}



- (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath{
    
    [UIView animateWithDuration:0.5 animations:^{
        cell.layer.transform = CATransform3DMakeScale(1.5, 1.5, 0);
        cell.layer.transform = CATransform3DMakeScale(1, 1, 1);
    } completion:^(BOOL finished) {
        
    }];
    
    
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return _arr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    MoreCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MoreCollectionViewCell" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor blackColor];
    
    CityModel *model = _arr[indexPath.row];
    
    cell.model = model;
    
    
    
    return cell;
}

- (void)handleData{
    
    [AFNetWorkingHandle GETRequestWithURLString:_url completionWithSuccess:^(id result) {
       
        NSLog(@"%@", result);
        NSArray *arr = result[@"data"];
        
        self.arr = [CityModel ModelHandleWithArray:arr];
        NSLog(@"%@", _arr);
        [_collection reloadData];
        
    } withFailure:^(id error) {
        
    }];
    
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
